// 当前绘制类型
let draw_tyoe = ''
// 第一个点的坐标
let point_star
// 第二个点的坐标
let point_end
// 第三个点的坐标
let point_pedal
// 第一个点的实体
let start_entity = null
// 第二个点的实体
let end_entity = null
// 第三个点实体
let pedal_entity = null
// 起点夹角实体
let angle_star_entity = null
// 终点夹角实体
let angle_end_entity = null
// 面积实体
let area_entity = null
// 面积标签
let area_label = null
// 中点
let midpoint
// 编辑时选择的点
let redact = null
// 多线段的夹角
let angle_multisegment = null
// 长度实体
let length_entity = null
// 直角虚线 高（起）
let height_start = null
// 直角虚线 高（起）标签
let height_start_label = null
// 直角虚线 长（终）
let height_end = null
// 直角虚线 长（终）标签
let height_end_label = null
// 起始点连接的线的实体
let line_entity = null
// 斜边标签
let hypotenuse_label = null
// 存储所有图形的数据
let entity_arry = new Map()
// 存储图形的坐标数组
let line_point = []
// 临时存储的坐标
let current_point = []
// 面积测量时存储所有的点
let area_point = []
// 当前id
let id = ''
let name = ''
let line_click = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
let line_moce = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
let line_reight = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
let EDITOR_click = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
let EDITOR_moce = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
let EDITOR_reight = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas)
/**
 * 开启测量
 * @constructor
 */
const Open_measurement = (type) => {
    Exit_edit()
    switch (type){
        // 线段
        case 'line':
            line_click.setInputAction((movement) => Straight_line_click(movement),Cesium.ScreenSpaceEventType.LEFT_CLICK)
            break
        // 多线段
        case 'multisegment':
            line_click.setInputAction((movement) => Straight_multisegment_click(movement),Cesium.ScreenSpaceEventType.LEFT_CLICK)
            line_reight.setInputAction((movement) => Straight_multisegment_right(movement),Cesium.ScreenSpaceEventType.RIGHT_DOWN)
            break
        // 综合测量
        case 'high':
            line_click.setInputAction((movement) => Straight_high_click(movement),Cesium.ScreenSpaceEventType.LEFT_CLICK)
            break
        // 高度测量
        case 'perpendicularity':
            line_click.setInputAction((movement) => Straight_perpendicularity_click(movement),Cesium.ScreenSpaceEventType.LEFT_CLICK)
            break
        // 面积测量
        case 'area':
            line_click.setInputAction((movement) => Straight_area_click(movement),Cesium.ScreenSpaceEventType.LEFT_CLICK)
            line_reight.setInputAction((movement) => Straight_area_right(movement),Cesium.ScreenSpaceEventType.RIGHT_DOWN)
            break

    }
}

/**
 * 开启编辑模式
 * @constructor
 */
const Editing_model = () => {
    line_click.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_CLICK)
    EDITOR_click.setInputAction((movement) => {
        let pickedObject = viewer.scene.pick(movement.position);
        if (Cesium.defined(pickedObject) &&
            Cesium.defined(pickedObject.id) &&
            pickedObject.id.point
        ){
            redact = pickedObject.id
            redact.point.color = Cesium.Color.BLUE
            // 关闭相机的控制
            viewer.scene.screenSpaceCameraController.enableInputs = false;
        }
    },Cesium.ScreenSpaceEventType.LEFT_DOWN)
    EDITOR_reight.setInputAction(() => {
        if (Cesium.defined(redact)
        ){
            redact.point.color = Cesium.Color.WHEAT
            // 关闭相机的控制
            viewer.scene.screenSpaceCameraController.enableInputs = true;
            redact = null
        }
    },Cesium.ScreenSpaceEventType.LEFT_UP)
    EDITOR_moce.setInputAction((movement) => {
        if (Cesium.defined(redact)
        ){
            let now_point = getLocation(movement.endPosition)
            if(now_point === false){
                return
            }
            let arr = redact.id.split('_')
            let data = entity_arry.get(arr[1] + arr[0])
            // 第几个点
            let table = arr[2]
            // console.log(entity_arry)
            // console.log(data)
            // console.log(table)
            // console.log(redact.id)
            switch (arr[1]){
                case 'multisegment':
                    // 编辑直线
                    Edit_multisegment(now_point,arr,data,table)
                    break
                case 'line' :
                    // 编辑线段
                    Edit_line(now_point,table,data)
                    break
                case 'high' :
                    // 编辑综合测量
                    Edit_high(now_point,table,data)
                    break
                case 'perpendicularity':
                    // 垂直
                    Edit_perpendicularity(now_point,table,data)
                    break
                case 'area' :
                    // 面积
                    Edit_area(now_point,table,data)
                    break
            }
        }

    },Cesium.ScreenSpaceEventType.MOUSE_MOVE)
}
/**
 * 退出编辑模式
 * @constructor
 */
const Exit_edit = () => {
    EDITOR_click.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_DOWN)
    EDITOR_reight.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_UP)
    EDITOR_moce.removeInputAction(Cesium.ScreenSpaceEventType.MOUSE_MOVE)
}


/**
 * 绘制面积的开始方法
 * @param movement
 * @constructor
 */
const Straight_area_click = (movement) => {
    let pickedObject = viewer.scene.pick(movement.position);
    if (Cesium.defined(pickedObject) &&
        Cesium.defined(pickedObject.id) &&
        current_point.length >= 2
    ){
        end_entity.position = point_end
        // 长度实体标签
        line_entity.polyline.positions = [point_star,point_end]
        area_point.push(end_entity)
        line_point.push(line_entity)
        point_pedal = point_star
        point_star = point_end
        point_end = null
        end_entity = null
        line_entity = null
        return
    }
    point_star = getLocation(movement.position)
    if(point_star === false){
        return;
    }
    current_point.push(point_star)
    // 创建第一个点
    start_entity = drawPointLabel(point_star,entity_arry.size + '_area' + '_' +current_point.length,'area')
    area_point.push(start_entity)
    line_moce.setInputAction((movement) => Straight_area_move(movement),Cesium.ScreenSpaceEventType.MOUSE_MOVE)
}

/**
 * 绘制面积移动的方法
 * @param movement
 * @constructor
 */
const Straight_area_move = (movement) => {
    let now_point = getLocation(movement.endPosition)
    if(now_point === false){
        return
    }
    point_end = now_point
    if(current_point.length >= 1 && end_entity === null && line_entity === null){
        current_point.push(point_end)
        id = entity_arry.size+'_area' + '_' +current_point.length
        // 创建第二个点
        end_entity = drawPointLabel(point_end,entity_arry.size+'_area' + '_' +current_point.length,'area')
        name = 'multisegment'
        // 创建线
        line_entity = drawPolyline(current_point,entity_arry.size+'_area_line' + '_'+line_point.length,'area')
        end_entity.position =  new Cesium.CallbackProperty(()=>{
            current_point[current_point.length - 1] = point_end
            return  point_end
        })
        line_entity.polyline.positions = new Cesium.CallbackProperty(()=>{
            return  [point_star,point_end]
        })
        // 创建面积
        if(current_point.length > 2) {
            if(area_entity === null){
                area_entity = drawPolygon(current_point,entity_arry.size+'_area_entity','area')
                area_entity.polygon.hierarchy= new Cesium.CallbackProperty(()=>{
                    return  new Cesium.PolygonHierarchy(current_point)
                })
            }
            if(area_label === null){
                area_label = addLabel(current_point[0],getArea(current_point)+"㎡",entity_arry.size+'_area_label' + '_'+line_point.length,'area')
                area_label.label.text = new Cesium.CallbackProperty(()=>{
                    return  getArea(current_point)+"㎡"
                })
            }
        }
    }
}

/**
 * 右键结束面积绘制
 * @param movement
 * @constructor
 */
const Straight_area_right = (movement) => {
    let pickedObject = viewer.scene.pick(movement.position);
    if (Cesium.defined(pickedObject) &&
        Cesium.defined(pickedObject.id) &&
        area_point.length > 2
    ){
        if(end_entity !== null && line_entity !== null){
            viewer.entities.remove(end_entity)
            viewer.entities.remove(line_entity)
        }
        if(area_entity !== null){
            if(current_point.length - line_point.length > 1){
                current_point.pop()
            }
            area_entity.polygon.hierarchy = new Cesium.PolygonHierarchy(current_point)
        }
        if(area_label !== null){
            area_label.label.text = getArea(current_point)+"㎡"
        }
        // 创建首尾连接线
        let star_end_entity = drawPolyline([point_star,current_point[0]],entity_arry.size+'_area_line_star_end' + '_'+line_point.length,'area')
        line_point.push(star_end_entity)
        let data = {
            // 线实体标签
            Length: line_point,
            // 面积标签
            area_label: area_label,
            // 面积实体
            area_entity: area_entity,
            // 所有点的数组
            current_point: current_point,
            // 所有实体点
            area_point: area_point
        }
        entity_arry.set('area' + entity_arry.size,data)
        point_star = null
        point_end = null
        line_entity = null
        end_entity = null
        line_point = []
        current_point = []
        area_label = null
        area_entity = null

        line_moce.removeInputAction(Cesium.ScreenSpaceEventType.MOUSE_MOVE)
        // line_click.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_CLICK)
        // line_reight.removeInputAction(Cesium.ScreenSpaceEventType.RIGHT_DOWN)
    }
}



/**
 * 绘制直线的开始方法
 * @param movement
 * @constructor
 */
const Straight_multisegment_click = (movement) => {
    let pickedObject = viewer.scene.pick(movement.position);
    if (Cesium.defined(pickedObject) &&
        Cesium.defined(pickedObject.id) &&
        current_point.length >= 2
    ){
        end_entity.position = point_end
        line_entity.polyline.positions = [point_star,point_end]
        length_entity.position = Cesium.Cartesian3.midpoint(point_star,point_end,new Cesium.Cartesian3())
        if(angle_multisegment !== null){
            angle_multisegment.label.text = computeCZ(point_star,point_end,point_pedal)+"°"
        }
        length_entity.label.text = Cesium.Cartesian3.distance(point_star,point_end).toFixed(2) + 'm'
        let data = {
            // 线实体
            line: line_entity,
            // 长度实体标签
            Length: length_entity,
            // 夹角
            angle: angle_multisegment,
            // 起点
            start_entity: start_entity,
            // 终点
            end_entity: end_entity
        }
        line_point.push(data)
        point_pedal = point_star
        point_star = point_end
        point_end = null
        line_entity = null
        end_entity = null
        length_entity = null
        angle_multisegment = null
        return
    }
    point_star = getLocation(movement.position)
    if(point_star === false){
        return;
    }
    current_point.push(point_star)
    // 创建第一个点
    start_entity = drawPointLabel(point_star,entity_arry.size + '_multisegment' + '_' +current_point.length,'multisegment')
    line_moce.setInputAction((movement) => Straight_multisegment_move(movement),Cesium.ScreenSpaceEventType.MOUSE_MOVE)
}

/**
 * 绘制多线移动的方法
 * @param movement
 * @constructor
 */
const Straight_multisegment_move = (movement) => {
    let now_point = getLocation(movement.endPosition)
    if(now_point === false){
        return
    }
    point_end = now_point
    if(current_point.length >= 1 && end_entity === null && line_entity === null){
        current_point.push(point_end)
        id = entity_arry.size+'_multisegment' + '_' +current_point.length
        // 创建第二个点
        end_entity = drawPointLabel(point_end,entity_arry.size+'_multisegment' + '_' +current_point.length,'multisegment')
        name = 'multisegment'
        // 创建线
        line_entity = drawPolyline(current_point,entity_arry.size+'_multisegment_line' + '_'+line_point.length,'multisegment')
        end_entity.position =  new Cesium.CallbackProperty(()=>{
            current_point[current_point.length - 1] = point_end
            return  point_end
        })
        line_entity.polyline.positions = new Cesium.CallbackProperty(()=>{
            return  [point_star,point_end]
        })
        // 计算终点
        midpoint = Cesium.Cartesian3.midpoint(point_star,point_end,new Cesium.Cartesian3())
        // 创建长度的实体
        length_entity = addLabel(midpoint,Cesium.Cartesian3.distance(point_star,point_end).toFixed(2) + 'm',entity_arry.size+'_multisegment_label' + '_'+line_point.length)
        length_entity.position =  new Cesium.CallbackProperty(()=>{
            return Cesium.Cartesian3.midpoint(point_star,point_end,new Cesium.Cartesian3())
        })
        length_entity.label.text = new Cesium.CallbackProperty(() => {
            return Cesium.Cartesian3.distance(point_star,point_end).toFixed(2) + 'm'
        })
        // 创建夹角
        if(current_point.length > 2) {
            angle_multisegment = addLabel(point_end,computeCZ(point_star,point_end,point_pedal)+"°",entity_arry.size+'_multisegment_angle' + '_'+line_point.length,'multisegment')
            angle_multisegment.label.text = new Cesium.CallbackProperty(()=>{
                return  computeCZ(point_star,point_end,point_pedal)+"°"
            })
        }
    }
}

/**
 * 右键结束线段绘制
 * @param movement
 * @constructor
 */
const Straight_multisegment_right = (movement) => {
    let pickedObject = viewer.scene.pick(movement.position);
    if (Cesium.defined(pickedObject) &&
        Cesium.defined(pickedObject.id) &&
        current_point.length >= 2
    ){
        if(end_entity !== null && line_entity !== null){
            viewer.entities.remove(end_entity)
            viewer.entities.remove(line_entity)
        }
        if(angle_multisegment !== null){
            viewer.entities.remove(angle_multisegment)
        }
        if(length_entity !== null){
            viewer.entities.remove(length_entity)
        }
        entity_arry.set('multisegment' + entity_arry.size,line_point)
        // point_star = null
        // point_end = null
        line_entity = null
        end_entity = null
        length_entity = null
        line_point = []
        angle_multisegment = null
        current_point = []
        line_moce.removeInputAction(Cesium.ScreenSpaceEventType.MOUSE_MOVE)
        // line_click.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_CLICK)
        // line_reight.removeInputAction(Cesium.ScreenSpaceEventType.RIGHT_DOWN)
    }
}
/**
 * 创建直线线段
 * */
const Straight_line_click = (movement) => {
    let pickedObject = viewer.scene.pick(movement.position);
    if (Cesium.defined(pickedObject) &&
        Cesium.defined(pickedObject.id) &&
        current_point.length === 2
    ){
        end_entity.position = point_end
        line_entity.polyline.positions = current_point
        // 创建长度的实体
        length_entity.position = Cesium.Cartesian3.midpoint(point_star,point_end,new Cesium.Cartesian3())
        length_entity.label.text = Cesium.Cartesian3.distance(point_star,point_end).toFixed(2) + 'm'
        // let data = getLength(current_point[0],current_point[1])
        current_point = []
        point_star = null
        point_end = null
        let data = {
            // 线实体
            line: line_entity,
            // 长度实体标签
            Length: length_entity,
            // 第一个点
            start_entity: start_entity,
            // 第二个点
            end_entity: end_entity
        }
        entity_arry.set('line' + entity_arry.size,data)
        line_entity = null
        end_entity = null
        line_moce.removeInputAction(Cesium.ScreenSpaceEventType.MOUSE_MOVE)
        return
    }
    // point_star = getCatesian3FromPX(movement.position)
    point_star = getLocation(movement.position)
    if(point_star === false){
        return;
    }
    current_point.push(point_star)
    // 创建第一个点
    start_entity = drawPointLabel(point_star,entity_arry.size + '_line' + '_' +current_point.length,'line')
    line_moce.setInputAction((movement) => Straight_line_move(movement),Cesium.ScreenSpaceEventType.MOUSE_MOVE)

}
/**
 * 画直线时候移动的方法
 */
const Straight_line_move = (movement) => {
    // let now_point = getCatesian3FromPX(movement.endPosition)
    let now_point = getLocation(movement.endPosition)
    if(now_point === false){
        return
    }
    point_end = now_point
    if(current_point.length >= 1 && end_entity === null && line_entity === null){
        current_point.push(point_end)
        // 创建第二个点
        end_entity = drawPointLabel(point_end,entity_arry.size+'_line' + '_' +current_point.length,'line')
        id = entity_arry.size+'_line' + '_' +current_point.length
        name = 'line'
        // 创建线
        line_entity = drawPolyline(current_point,entity_arry.size+'_line_line' + '_'+line_point.length,'line')
        end_entity.position =  new Cesium.CallbackProperty(()=>{
            current_point[current_point.length - 1] = point_end
            return  point_end
        })
        line_entity.polyline.positions = new Cesium.CallbackProperty(()=>{
            return  [point_star,point_end]
        })
        // 创建长度的实体
        length_entity = addLabel(new Cesium.CallbackProperty(()=>{
                // 计算终点
            return  Cesium.Cartesian3.midpoint(point_star,point_end,new Cesium.Cartesian3())
        }),
            new Cesium.CallbackProperty(()=>{
                return  Cesium.Cartesian3.distance(point_star,point_end).toFixed(2) + 'm'
            }),
            entity_arry.size+'_line_length' + '_'+line_point.length,'line')
    }
}


/**
 * 综合测量开始
 * @param movement
 * @constructor
 */
const Straight_high_click = (movement) => {
    let pickedObject = viewer.scene.pick(movement.position);
    if (Cesium.defined(pickedObject) &&
        Cesium.defined(pickedObject.id) &&
        current_point.length === 2
    ){
        // line_point.push(current_point)
        current_point = []
        end_entity.position = point_end
        // let high_point = returnHeightPosition(point_end,point_star)
        pedal_entity.position = point_pedal
        line_entity.polyline.positions = [point_star,point_end]
        height_start.polyline.positions = [point_star,point_pedal]
        height_end.polyline.positions = [point_end,point_pedal]
        angle_star_entity.label.text = computeCZ(point_star,point_end,point_pedal)+"°"
        angle_end_entity.label.text = computeCZ(point_end,point_star,point_pedal)+"°"
        angle_end_entity.position = point_end
        length_entity.position = Cesium.Cartesian3.midpoint(point_star,point_end,new Cesium.Cartesian3())
        length_entity.label.text = Cesium.Cartesian3.distance(point_star,point_end).toFixed(2) + 'm'
        height_start_label.position = Cesium.Cartesian3.midpoint(point_star,point_pedal,new Cesium.Cartesian3())
        height_start_label.label.text = Cesium.Cartesian3.distance(point_star,point_pedal).toFixed(2) + 'm'
        height_end_label.position = Cesium.Cartesian3.midpoint(point_pedal,point_end,new Cesium.Cartesian3())
        height_end_label.label.text = Cesium.Cartesian3.distance(point_pedal,point_end).toFixed(2) + 'm'
        let data = {
            // 斜边
            line_entity: line_entity,
            // 直角边（起）
            height_start: height_start,
            // 直角边（终）
            height_end: height_end,
            // 起点角度
            angle_star_entity: angle_star_entity,
            // 终点角度
            angle_end_entity: angle_end_entity,
            // 边长（斜边）
            length_entity: length_entity,
            // 直角边（起）
            height_start_label: height_start_label,
            // 直角边（终）
            height_end_label:height_end_label,
            // 直角点的实体（第三点）
            pedal_entity:pedal_entity,
            // 起点
            start_entity: start_entity,
            // 终点
            end_entity: end_entity


        }
        entity_arry.set('high' + entity_arry.size,data)
        line_entity = null
        height_start = null
        height_end = null
        angle_star_entity = null
        angle_end_entity = null
        length_entity = null
        height_start_label = null
        height_end_label = null
        start_entity = null
        end_entity = null
        line_moce.removeInputAction(Cesium.ScreenSpaceEventType.MOUSE_MOVE)
        return
    }
    point_star = getLocation(movement.position)
    if(point_star === false){
        return;
    }
    current_point.push(point_star)
    // 创建第一个点
    start_entity = drawPointLabel(point_star,entity_arry.size + '_high' + '_' +current_point.length,'high')
    line_moce.setInputAction((movement) =>Straight_high_move(movement),Cesium.ScreenSpaceEventType.MOUSE_MOVE)
}

/**
 * 综合测量移动
 * @param movement
 * @constructor
 */
const Straight_high_move = (movement) => {
    let now_point = getLocation(movement.endPosition)
    if(now_point === false){
        return
    }
    point_end = now_point
    if(current_point.length >= 1 && end_entity === null && line_entity === null){
        current_point.push(point_end)
        // 创建第二个点
        end_entity = drawPointLabel(point_end,entity_arry.size+'_high' + '_' +current_point.length,'high')
        end_entity.position = new Cesium.CallbackProperty(()=>{
            current_point[current_point.length - 1] = point_end
            return  point_end
        })
        // 计算第三点
        point_pedal = returnHeightPosition(point_end,point_star)
        pedal_entity = drawPointLabel(point_pedal,entity_arry.size+'_highr' + '_' + (parseInt(current_point.length) + 1),'high')
        pedal_entity.position = new Cesium.CallbackProperty(()=>{
            point_pedal = returnHeightPosition(point_end,point_star)
            return  returnHeightPosition(point_end,point_star)
        })
        // 画直角边高（起）的虚线
        height_start = drawDashedLine([point_star,point_pedal],entity_arry.size+'_high_start' + '_' +current_point.length,'high')
        height_start.polyline.positions = new Cesium.CallbackProperty(()=>{
            return  [point_star,point_pedal]
        })
        // 画直角边高（终）的虚线
        height_end = drawDashedLine([point_end,point_pedal],entity_arry.size+'_high_end' + '_' +current_point.length,'high')
        height_end.polyline.positions = new Cesium.CallbackProperty(()=>{
            return  [point_end,point_pedal]
        })
        // 创建线
        line_entity = drawPolyline(current_point,entity_arry.size+'_high_line' + '_'+line_point.length,'high')
        line_entity.polyline.positions = new Cesium.CallbackProperty(()=>{
            return  [point_star,point_end]
        })
        // 创建长度的实体 起始点
        // 计算终点
        midpoint = Cesium.Cartesian3.midpoint(point_star,point_end,new Cesium.Cartesian3())
        length_entity = addLabel(midpoint,Cesium.Cartesian3.distance(point_star,point_end).toFixed(2) + 'm',entity_arry.size+'_high_table' + '_'+line_point.length,'high')
        length_entity.position = new Cesium.CallbackProperty(()=>{
            return  Cesium.Cartesian3.midpoint(point_star,point_end,new Cesium.Cartesian3())
        })
        length_entity.label.text = new Cesium.CallbackProperty(()=>{
            return  Cesium.Cartesian3.distance(point_star,point_end).toFixed(2) + 'm'
        })
        // 创建长度的实体(高)起
        midpoint = Cesium.Cartesian3.midpoint(point_star,point_pedal,new Cesium.Cartesian3())
        height_start_label = addLabel(midpoint,Cesium.Cartesian3.distance(point_star,point_pedal).toFixed(2) + 'm',entity_arry.size+'_high_table_start' + '_'+line_point.length,'high')
        height_start_label.position = new Cesium.CallbackProperty(()=>{
            return  Cesium.Cartesian3.midpoint(point_star,point_pedal,new Cesium.Cartesian3())
        })
        height_start_label.label.text = new Cesium.CallbackProperty(()=>{
            return  Cesium.Cartesian3.distance(point_star,point_pedal).toFixed(2) + 'm'
        })
        // 创建角度（起）
        angle_star_entity = addLabel(point_star,computeCZ(point_star,point_end,point_pedal)+"°",entity_arry.size+'_high_angle_start' + '_'+line_point.length,'high')
        angle_star_entity.label.text = new Cesium.CallbackProperty(()=>{
            return  computeCZ(point_star,point_end,point_pedal)+"°"
        })
        // 创建长度的实体(长)终
        midpoint = Cesium.Cartesian3.midpoint(point_end,point_pedal,new Cesium.Cartesian3())
        height_end_label = addLabel(midpoint,Cesium.Cartesian3.distance(point_end,point_pedal).toFixed(2) + 'm',entity_arry.size+'_high_table_end' + '_'+line_point.length,'high')
        height_end_label.position = new Cesium.CallbackProperty(()=>{
            return  Cesium.Cartesian3.midpoint(point_end,point_pedal,new Cesium.Cartesian3())
        })
        height_end_label.label.text = new Cesium.CallbackProperty(()=>{
            return  Cesium.Cartesian3.distance(point_end,point_pedal).toFixed(2) + 'm'
        })
        // 创建角度（终）
        angle_end_entity = addLabel(point_end,computeCZ(point_end,point_pedal,point_star)+"°",entity_arry.size+'_high_angle_end' + '_'+line_point.length,'high')
        angle_end_entity.label.text = new Cesium.CallbackProperty(()=>{
            return  computeCZ(point_end,point_pedal,point_star)+"°"
        })
        angle_end_entity.position = new Cesium.CallbackProperty(()=>{
            return  point_end
        })
        id = entity_arry.size+'_high' + '_' +current_point.length
        name = 'high'

    }
}

/**
 * 垂直测量开始
 * @param movement
 * @constructor
 */
const Straight_perpendicularity_click = (movement) => {
    let pickedObject = viewer.scene.pick(movement.position);
    if (Cesium.defined(pickedObject) &&
        Cesium.defined(pickedObject.id) &&
        current_point.length === 2
    ){
        // line_point.push(current_point)
        current_point = []
        end_entity.position = point_end
        // let high_point = returnHeightPosition(point_end,point_star)
        pedal_entity.position = point_pedal
        height_start.polyline.positions = [point_star,point_pedal]
        height_end.polyline.positions = [point_end,point_pedal]
        // height_start_label.position = Cesium.Cartesian3.midpoint(point_star,point_pedal,new Cesium.Cartesian3())
        // height_start_label.label.text = Cesium.Cartesian3.distance(point_star,point_pedal).toFixed(2) + 'm'
        height_end_label.position = Cesium.Cartesian3.midpoint(point_pedal,point_end,new Cesium.Cartesian3())
        height_end_label.label.text = Cesium.Cartesian3.distance(point_pedal,point_end).toFixed(2) + 'm'
        let data = {
            // 直角边（起）
            height_start: height_start,
            // 直角边（终）
            height_end: height_end,
            // 直角边（起）
            // height_start_label: height_start_label,
            // 直角边（终）
            height_end_label:height_end_label,
            // 直角点的实体（第三点）
            pedal_entity:pedal_entity,
            //起点
            start_entity: start_entity,
            // 终点
            end_entity: end_entity

        }
        entity_arry.set('perpendicularity' + entity_arry.size,data)
        height_start = null
        height_end = null
        height_start_label = null
        height_end_label = null
        start_entity = null
        end_entity = null
        line_moce.removeInputAction(Cesium.ScreenSpaceEventType.MOUSE_MOVE)
        return
    }
    point_star = getLocation(movement.position)
    if(point_star === false){
        return;
    }
    current_point.push(point_star)
    // 创建第一个点
    start_entity = drawPointLabel(point_star,entity_arry.size + '_perpendicularity' + '_' +current_point.length,'perpendicularity')
    line_moce.setInputAction((movement) =>Straight_perpendicularity_move(movement),Cesium.ScreenSpaceEventType.MOUSE_MOVE)
}
/**
 * 垂直测量移动
 * @param movement
 * @constructor
 */
const Straight_perpendicularity_move = (movement) => {
    let now_point = getLocation(movement.endPosition)
    if(now_point === false){
        return
    }
    point_end = now_point
    if(current_point.length >= 1 && end_entity === null){
        current_point.push(point_end)
        // 创建第二个点
        end_entity = drawPointLabel(point_end,entity_arry.size+'_perpendicularity' + '_' +current_point.length,'perpendicularity')
        end_entity.position = new Cesium.CallbackProperty(()=>{
            current_point[current_point.length - 1] = point_end
            return  point_end
        })
        // 计算第三点
        point_pedal = returnHeightPosition(point_end,point_star)
        pedal_entity = drawPointLabel(point_pedal,entity_arry.size+'_perpendicularity' + '_' + (parseInt(current_point.length) + 1),'perpendicularity')
        pedal_entity.position = new Cesium.CallbackProperty(()=>{
            point_pedal = returnHeightPosition(point_end,point_star)
            return  returnHeightPosition(point_end,point_star)
        })
        // 画直角边高（起）的虚线
        height_start = drawDashedLine([point_star,point_pedal],entity_arry.size+'_perpendicularity_start' + '_' +current_point.length,'perpendicularity')
        height_start.polyline.positions = new Cesium.CallbackProperty(()=>{
            return  [point_star,point_pedal]
        })
        // 画直角边高（终）的虚线
        height_end = drawDashedLine([point_end,point_pedal],entity_arry.size+'_perpendicularity_end' + '_' +current_point.length,'perpendicularity')
        height_end.polyline.positions = new Cesium.CallbackProperty(()=>{
            return  [point_end,point_pedal]
        })
        // 创建长度的实体(高)起
        // midpoint = Cesium.Cartesian3.midpoint(point_star,point_pedal,new Cesium.Cartesian3())
        // height_start_label = addLabel(midpoint,Cesium.Cartesian3.distance(point_star,point_pedal).toFixed(2) + 'm',entity_arry.size+'_perpendicularity_table_start' + '_'+line_point.length,'high')
        // height_start_label.position = new Cesium.CallbackProperty(()=>{
        //     return  Cesium.Cartesian3.midpoint(point_star,point_pedal,new Cesium.Cartesian3())
        // })
        // height_start_label.label.text = new Cesium.CallbackProperty(()=>{
        //     return  Cesium.Cartesian3.distance(point_star,point_pedal).toFixed(2) + 'm'
        // })
        // 创建长度的实体(长)终
        midpoint = Cesium.Cartesian3.midpoint(point_end,point_pedal,new Cesium.Cartesian3())
        height_end_label = addLabel(midpoint,Cesium.Cartesian3.distance(point_end,point_pedal).toFixed(2) + 'm',entity_arry.size+'_perpendicularity_table_end' + '_'+line_point.length,'high')
        height_end_label.position = new Cesium.CallbackProperty(()=>{
            return  Cesium.Cartesian3.midpoint(point_end,point_pedal,new Cesium.Cartesian3())
        })
        height_end_label.label.text = new Cesium.CallbackProperty(()=>{
            return  Cesium.Cartesian3.distance(point_end,point_pedal).toFixed(2) + 'm'
        })
        id = entity_arry.size+'_perpendicularity' + '_' +current_point.length
        name = 'perpendicularity'

    }
}



/**
 * 名称：绘制多段线
 * 作用：通过传入的三个点来计算其角度
 * 参数：position：开始
 * position：坐标1
 * 状态：
 * 网址：https://blog.csdn.net/qq_40323256/article/details/128178560
 * 负责人：
 */
function drawPolyline(positions,id,name) {
    return viewer.entities.add({
        id: id,
        name: name,
        polyline: {
            positions: positions,
            width: 2,
            material: new Cesium.PolylineGlowMaterialProperty({
                color: Cesium.Color.WHEAT
            }),
            depthFailMaterial: new Cesium.PolylineGlowMaterialProperty({
                color: Cesium.Color.WHEAT
            }),
            // clampToGround: true // 贴地
            zIndex: 1
        }
    })
}
/**
 * 名称：绘制点
 * 作用：通过传入的三个点来计算其角度
 * 参数：position：开始，pointNum
 * position：坐标1
 * pointNum：坐标1
 * 状态：
 * 网址：https://blog.csdn.net/qq_40323256/article/details/128178560
 * 负责人：
 */
function drawPointLabel(position, id,name) {
    // 本质上就是添加一个点的实体
    return viewer.entities.add({
        id: id,
        name: name,
        position: position,
        point: {
            color: Cesium.Color.WHEAT,
            pixelSize: 5,
            outlineWidth: 3,
            disableDepthTestDistance: Number.POSITIVE_INFINITY
        }
    })
}


/**
 * 名称：返回两条线段的夹角
 * 说明：通过传入的三点返回两线之间的夹角
 * 参数：p1：点1, p2：点2 p3：点3
 * 状态：已完成
 * 负责人：/
 * 网址：
 */
function computeCZ (p1, p2, p3) {
    // 得到 p1p2 的方向向量
    var c1 = Cesium.Cartesian3.subtract(p2, p1, new Cesium.Cartesian3());
    var c2 = Cesium.Cartesian3.subtract(p3, p1, new Cesium.Cartesian3());
    // 计算两向量的夹角
    var angle = Cesium.Cartesian3.angleBetween(c1, c2);
    // var dis = Cesium.Cartesian3.distance(p1, p3);
    // console.log(Math.cos(angle))
    // dis = dis * Math.cos(angle);
    // var nor = Cesium.Cartesian3.normalize(c1, new Cesium.Cartesian3());
    // var newC = Cesium.Cartesian3.multiplyByScalar(nor, dis, new Cesium.Cartesian3());
    // var res = Cesium.Cartesian3.add(newC, p1, new Cesium.Cartesian3());
    // console.log(angle,dis,nor,newC,res)
    return ((angle * 180) / Math.PI).toFixed(2);
}

/**
 * 名称：创建实体标签
 * 作用：创建实体标签
 * 参数：midPoint：开始，labelLength:结束
 * midPoint：坐标，
 * labelLength：距离
 * 状态：
 * 网址：https://blog.csdn.net/qq_40323256/article/details/128178560
 * 负责人：
 */
function addLabel(midPoint, labelLength, id,name) {
    return viewer.entities.add({
        id: id,
        name: name,
        position: midPoint,
        label: {
            text: labelLength,
            font: '20px sans-serif',
            fillColor: Cesium.Color.WHITE,
            outlineWidth: 2,
            backgroundColor: Cesium.Color.BLACK,
            showBackground: true,
            style: Cesium.LabelStyle.FILL,
            verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
            horizontalOrigin: Cesium.HorizontalOrigin.CENTER,
            // heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
            disableDepthTestDistance: Number.POSITIVE_INFINITY
        }
    })
}

function getLength(start, end) {
    // 将起点与终点位置信息从笛卡尔坐标形式转换为Cartographic形式
    let startCartographic = Cesium.Cartographic.fromCartesian(start)
    let endCartographic = Cesium.Cartographic.fromCartesian(end)
    // 初始化测地线
    let geodesic = new Cesium.EllipsoidGeodesic()
    // 设置测地线起点和终点，EllipsoidGeodesic中setEndPoints常与surfaceDistance搭配使用
    geodesic.setEndPoints(startCartographic, endCartographic)
    return (geodesic.surfaceDistance).toFixed(2)
}

/**
 * 名称：返回两点的高度坐标
 * 说明：通过传入的两点返回两点之间的高度坐标
 * 参数：positionsone：起点, positionstwo：终点
 * 状态：已完成
 * 负责人：/
 * 网址：https://blog.csdn.net/qq_40323256/article/details/128391607
 */
function returnHeightPosition(positionsone, positionstwo){
    let point1cartographic = Cesium.Cartographic.fromCartesian(positionsone);
    let point2cartographic = Cesium.Cartographic.fromCartesian(positionstwo);
    let point_temp = Cesium.Cartesian3.fromDegrees(Cesium.Math.toDegrees(point1cartographic.longitude),Cesium.Math.toDegrees(point1cartographic.latitude),point2cartographic.height);
    return point_temp
}

/**
 * 名称：绘制虚线
 * 作用：通过传入的参数绘制一条虚线
 * 参数：position：坐标
 * position：坐标1
 * 状态：
 * 网址：https://blog.csdn.net/qq_40323256/article/details/128178560
 * 负责人：
 */
function drawDashedLine(positions,id,name){
    if (positions.length < 1) return
    var dashedLine = viewer.entities.add({
        id: id,
        name: name,
        polyline: {
            positions: positions,
            width: 3,
            material: new Cesium.PolylineDashMaterialProperty({
                color: Cesium.Color.YELLOW,
                dashLength: 20 //短划线长度
            }),
            depthFailMaterial: new Cesium.PolylineGlowMaterialProperty({
                // eslint-disable-next-line new-cap
                color: Cesium.Color.WHEAT
            })
        }
    })
    return dashedLine
}


/**
 * 编辑直线
 * @param now_point 当前鼠标点
 * @param arr  点击的点的id
 * @param data 当前点击的点所在的线条
 * @param table 第几个点
 * @constructor
 */
const Edit_multisegment = (now_point,arr,data,table) => {
    if(data.length < table){
        // 获取最后一条数据
        let last_data = data[table - 2]
        last_data.line.polyline.positions = [last_data.line.polyline.positions._value[0],now_point]
        last_data.Length.label.text  = Cesium.Cartesian3.distance(last_data.line.polyline.positions._value[0],now_point).toFixed(2) + 'm'
        last_data.Length.position =  Cesium.Cartesian3.midpoint(last_data.line.polyline.positions._value[0],now_point,new Cesium.Cartesian3())
        if(data.length > 1){
            // 获取前一条数据
            let second_data = data[table - 3]
            last_data.angle.label.text = computeCZ(last_data.line.polyline.positions._value[0],now_point,second_data.line.polyline.positions._value[0])+"°"
        }
        // 修改选择的点的坐标
        redact.position = now_point
    } else {
        // 获取当前数据
        let Current_data = data[table - 1]
        // 修改移动的点
        redact.position = now_point
        // 修改当前线段的长度
        Current_data.line.polyline.positions = [now_point,Current_data.line.polyline.positions._value[1]]
        // 修改当前线段的长度实体的文本和点位
        Current_data.Length.label.text  = Cesium.Cartesian3.distance(Current_data.line.polyline.positions._value[1],now_point).toFixed(2) + 'm'
        Current_data.Length.position =  Cesium.Cartesian3.midpoint(Current_data.line.polyline.positions._value[1],now_point,new Cesium.Cartesian3())
        if(parseInt(table) - 1 > 0){
            // 获取前一条数据
            let last_data = data[table - 2]
            // 修改前一条线段的长度实体的文本和点位
            last_data.Length.label.text  = Cesium.Cartesian3.distance(last_data.line.polyline.positions._value[0],now_point).toFixed(2) + 'm'
            last_data.Length.position =  Cesium.Cartesian3.midpoint(last_data.line.polyline.positions._value[0],now_point,new Cesium.Cartesian3())
            // 修改前一条线段的长度
            last_data.line.polyline.positions = [last_data.line.polyline.positions._value[0],now_point]
            // 修改当前角度
            Current_data.angle.label.text = computeCZ(now_point,Current_data.line.polyline.positions._value[1],last_data.line.polyline.positions._value[0])+"°"
            Current_data.angle.position = now_point
            if(parseInt(table) - 2 > 0){
                // 获取前二条数据
                let third_data = data[table - 3]
                // 修改前二条的角度
                last_data.angle.label.text = computeCZ(third_data.line.polyline.positions._value[1],third_data.line.polyline.positions._value[0],now_point)+"°"
            }
        }
        if(parseInt(table) + 1 < data.length){
            // 获取后一条数据
            let second_data = data[table]
            // 修改后一条线段的长度实体的文本和点位
            second_data.angle.label.text = computeCZ(second_data.line.polyline.positions._value[0],second_data.line.polyline.positions._value[1],now_point)+"°"
        }
    }
}
/**
 * 编辑线段
 * @param now_point 当前点的坐标
 * @param table 点击的第几个坐标
 * @constructor
 */
const Edit_line = (now_point,table,data) => {
    // 修改点击的点的位置
    redact.position = now_point
    if(parseInt(table) === 1){
        data.line.polyline.positions = [now_point,data.line.polyline.positions._value[1]]
        data.Length.position = Cesium.Cartesian3.midpoint(data.line.polyline.positions._value[1],now_point,new Cesium.Cartesian3())
        data.Length.label.text = Cesium.Cartesian3.distance(data.line.polyline.positions._value[1],now_point).toFixed(2) + 'm'
    }else {
        data.line.polyline.positions = [data.line.polyline.positions._value[0],now_point]
        data.Length.position = Cesium.Cartesian3.midpoint(data.line.polyline.positions._value[0],now_point,new Cesium.Cartesian3())
        data.Length.label.text = Cesium.Cartesian3.distance(data.line.polyline.positions._value[0],now_point).toFixed(2) + 'm'
    }
}



/**
 * 编辑综合测量
 * @param now_point 当前点的位置
 * @param table 选中第几个点
 * @param data 当前点所在的数据
 * @constructor
 */
const Edit_high = (now_point,table,data) => {
    // 是否点击的起点   table = 1 为起点
    if (parseInt(table) === 1){
        // 修改点击的点
        redact.position = now_point
        // 计算第三点
        let pedal = returnHeightPosition(data.line_entity.polyline.positions._value[1],now_point)
        // 移动第三点的位置
        data.pedal_entity.position = pedal
        // 修改斜边实体
        data.line_entity.polyline.positions = [now_point,data.line_entity.polyline.positions._value[1]]
        // 修改斜边的距离
        data.length_entity.position = Cesium.Cartesian3.midpoint(now_point,data.line_entity.polyline.positions._value[1],new Cesium.Cartesian3())
        data.length_entity.label.text = Cesium.Cartesian3.distance(now_point,data.line_entity.polyline.positions._value[1]).toFixed(2) + 'm'
        // 修改起点角度
        data.angle_star_entity.position = now_point
        data.angle_star_entity.label.text = computeCZ(now_point,data.line_entity.polyline.positions._value[1],pedal)+"°"
        // 修改直角边（起）的实体
        data.height_start.polyline.positions = [now_point,pedal]
        // 修改直角边（起）的距离
        data.height_start_label.position =Cesium.Cartesian3.midpoint(now_point,pedal,new Cesium.Cartesian3())
        data.height_start_label.label.text = Cesium.Cartesian3.distance(now_point,pedal).toFixed(2) + 'm'

        // 修终起点角度
        data.angle_end_entity.label.text = computeCZ(data.line_entity.polyline.positions._value[1],now_point,pedal)+"°"
        // 修改直角边（终）的实体
        data.height_end.polyline.positions = [data.line_entity.polyline.positions._value[1],pedal]
        // 修改直角边（终）的距离
        data.height_end_label.position =Cesium.Cartesian3.midpoint(data.line_entity.polyline.positions._value[1],pedal,new Cesium.Cartesian3())
        data.height_end_label.label.text = Cesium.Cartesian3.distance(data.line_entity.polyline.positions._value[1],pedal).toFixed(2) + 'm'
    }else if(parseInt(table) === 2){
        // 修改点击的点
        redact.position = now_point
        // 计算第三点
        let pedal = returnHeightPosition(now_point,data.line_entity.polyline.positions._value[0])
        // 移动第三点的位置
        data.pedal_entity.position = pedal
        // 修改斜边实体
        data.line_entity.polyline.positions = [data.line_entity.polyline.positions._value[0],now_point]
        // 修改斜边的距离
        data.length_entity.position = Cesium.Cartesian3.midpoint(now_point,data.line_entity.polyline.positions._value[0],new Cesium.Cartesian3())
        data.length_entity.label.text = Cesium.Cartesian3.distance(now_point,data.line_entity.polyline.positions._value[0]).toFixed(2) + 'm'
        // 修改起点角度
        // data.angle_star_entity.position = data.line_entity.polyline.positions._value[0]
        data.angle_star_entity.label.text = computeCZ(data.line_entity.polyline.positions._value[0],now_point,pedal)+"°"
        // 修改直角边（起）的实体
        data.height_start.polyline.positions = [data.line_entity.polyline.positions._value[0],pedal]
        // 修改直角边（起）的距离
        data.height_start_label.position =Cesium.Cartesian3.midpoint(data.line_entity.polyline.positions._value[0],pedal,new Cesium.Cartesian3())
        data.height_start_label.label.text = Cesium.Cartesian3.distance(data.line_entity.polyline.positions._value[0],pedal).toFixed(2) + 'm'

        // 修终起点角度
        data.angle_end_entity.label.text = computeCZ(now_point,data.line_entity.polyline.positions._value[0],pedal)+"°"
        data.angle_end_entity.position = now_point
        // 修改直角边（终）的实体
        data.height_end.polyline.positions = [now_point,pedal]
        // 修改直角边（终）的距离
        data.height_end_label.position =Cesium.Cartesian3.midpoint(now_point,pedal,new Cesium.Cartesian3())
        data.height_end_label.label.text = Cesium.Cartesian3.distance(now_point,pedal).toFixed(2) + 'm'
    }
}

/**
 * 编辑高度测量
 * @param now_point 当前点的位置
 * @param table 选中第几个点
 * @param data 当前点所在的数据
 * @constructor
 */
const Edit_perpendicularity = (now_point,table,data) => {
    // 是否点击的起点   table = 1 为起点
    if (parseInt(table) === 1){
        // 修改点击的点
        redact.position = now_point
        // 计算第三点
        let pedal = returnHeightPosition(data.height_end.polyline.positions._value[0],now_point)
        // 移动第三点的位置
        data.pedal_entity.position = pedal
        // 修改直角边（起）的实体
        data.height_start.polyline.positions = [now_point,pedal]
        // 修改直角边（起）的距离
        // data.height_start_label.position =Cesium.Cartesian3.midpoint(now_point,pedal,new Cesium.Cartesian3())
        // data.height_start_label.label.text = Cesium.Cartesian3.distance(now_point,pedal).toFixed(2) + 'm'
        // 修改直角边（终）的实体
        data.height_end.polyline.positions = [data.height_end.polyline.positions._value[0],pedal]
        // 修改直角边（终）的距离
        data.height_end_label.position =Cesium.Cartesian3.midpoint(data.height_end.polyline.positions._value[0],pedal,new Cesium.Cartesian3())
        data.height_end_label.label.text = Cesium.Cartesian3.distance(data.height_end.polyline.positions._value[0],pedal).toFixed(2) + 'm'
    }else if(parseInt(table) === 2){
        // 修改点击的点
        redact.position = now_point
        // 计算第三点
        let pedal = returnHeightPosition(now_point,data.height_start.polyline.positions._value[0])
        // 移动第三点的位置
        data.pedal_entity.position = pedal
        // 修改直角边（起）的实体
        data.height_start.polyline.positions = [data.height_start.polyline.positions._value[0],pedal]
        // 修改直角边（起）的距离
        // data.height_start_label.position =Cesium.Cartesian3.midpoint(data.height_start.polyline.positions._value[0],pedal,new Cesium.Cartesian3())
        // data.height_start_label.label.text = Cesium.Cartesian3.distance(data.height_start.polyline.positions._value[0],pedal).toFixed(2) + 'm'
        // 修改直角边（终）的实体
        data.height_end.polyline.positions = [now_point,pedal]
        // 修改直角边（终）的距离
        data.height_end_label.position =Cesium.Cartesian3.midpoint(now_point,pedal,new Cesium.Cartesian3())
        data.height_end_label.label.text = Cesium.Cartesian3.distance(now_point,pedal).toFixed(2) + 'm'
    }
}

/**
 * 编辑面积
 * @param now_point 当前点的位置
 * @param table 选中第几个点
 * @param data 当前点所在的数据
 * @constructor
 */
const Edit_area = (now_point,table,data) => {
    // 获取所有的线数组
    let lineArr = data.Length
    // 点的第几个点
    let num = parseInt(table)
    redact.position = now_point
    lineArr[num - 1].polyline.positions = [now_point,lineArr[num - 1].polyline.positions._value[1]]
    // 点的第一个点
    if(num === 1){
        lineArr[lineArr.length - 1].polyline.positions = [lineArr[lineArr.length - 1].polyline.positions._value[0],now_point]
        // 更新标签位置
        data.area_label.position = now_point
    }else {
        lineArr[num - 2].polyline.positions = [lineArr[num - 2].polyline.positions._value[0],now_point]
    }
    // 原图形所有的点集合
    let arr_point = data.current_point
    // 更新点集合
    arr_point[num - 1] = now_point
    // 更新面实体
    data.area_entity.polygon.hierarchy = new Cesium.PolygonHierarchy(arr_point)
    // 更新标签类容
    data.area_label.label.text = getArea(arr_point)+"㎡"
}

/**
 * 名称：绘制多边形
 * 作用：通过传入的三个点来计算其角度
 * 参数：position：开始
 * position：坐标1
 * 状态：
 * 网址：https://blog.csdn.net/qq_40323256/article/details/128178560
 * 负责人：
 */
function drawPolygon(positions,id,name) {
    return viewer.entities.add({
        id: id,
        name: name,
        polygon: {
            hierarchy: positions,
            width: 5.0,
            // eslint-disable-next-line new-cap
            material: new Cesium.ColorMaterialProperty(
                Cesium.Color.WHEAT.withAlpha(0.4)
            ),
            // 给予多边形高度，不然就会是贴在模型上的现象
            perPositionHeight: true,
            zIndex: 2
        }
    })
}

/**
 * 名称：获取多边形
 * 作用：通过传入的三个点来计算其角度
 * 参数：positions：开始
 * positions：坐标1
 * 状态：
 * 网址：https://blog.csdn.net/qq_40323256/article/details/128178560
 * 负责人：
 */
function getArea(positions) {
    let res = 0
    for (let i = 0; i < positions.length - 2; i++) {
        let j = (i + 1) % positions.length
        let k = (i + 2) % positions.length
        let totalAngle = pointAngle(positions[i], positions[j], positions[k])
        let tempLength1 = getLength(positions[j], positions[0])
        let tempLength2 = getLength(positions[k], positions[0])
        res += tempLength1 * tempLength2 * Math.sin(totalAngle) / 2
    }
    res = res.toFixed(2)
    res = parseFloat(res)
    return Math.abs(res)
}



// 角度
/**
 * 名称：测量角度
 * 作用：通过传入的三个点来计算其角度
 * 参数：point1：开始，point2:结束，point3:结束
 * point1：坐标1
 * point2：坐标2
 * point3：坐标3
 * 状态：
 * 网址：https://blog.csdn.net/qq_40323256/article/details/128178560
 * 负责人：
 */
function pointAngle(point1, point2, point3) {
    let bearing21 = Bearing(point2, point1)
    let bearing23 = Bearing(point2, point3)
    let angle = bearing21 - bearing23
    if (angle < 0) {
        angle += Math.PI * 2.0
    }
    return angle
}

/**
 * 名称：测量空间面积
 * 作用：通过点位测量空间面积
 * 参数：from：开始，to:结束
 * from：坐标，
 * to：距离
 * 状态：
 * 网址：https://blog.csdn.net/qq_40323256/article/details/128178560
 * 负责人：
 */
function Bearing(from, to) {
    let fromCartographic = Cesium.Cartographic.fromCartesian(from)
    let toCartographic = Cesium.Cartographic.fromCartesian(to)
    let lat1 = fromCartographic.latitude
    let lon1 = fromCartographic.longitude
    let lat2 = toCartographic.latitude
    let lon2 = toCartographic.longitude
    let angle = -Math.atan2(Math.sin(lon1 - lon2) * Math.cos(lat2), Math.cos(lat1) * Math.sin(lat2) - Math.sin(lat1) * Math.cos(lat2) * Math.cos(lon1 - lon2))
    if (angle < 0) {
        angle += Math.PI * 2.0
    }
    return angle
}




/**
 * 清空所有数据
 * @constructor
 */
const Clear_draw = () => {
    for (let key of entity_arry.keys()) {
        // 当前存储的数据
        let graph = entity_arry.get(key)
        key = key.replace(/[0-9]+/g,"")
        switch (key){
            // 线段
            case 'line':
                viewer.entities.remove(graph.line)
                viewer.entities.remove(graph.Length)
                viewer.entities.remove(graph.start_entity)
                viewer.entities.remove(graph.end_entity)
                break
            // 多线段
            case 'multisegment':
                for (let graphElement of graph) {
                    viewer.entities.remove(graphElement.line)
                    viewer.entities.remove(graphElement.Length)
                    viewer.entities.remove(graphElement.angle)
                    viewer.entities.remove(graphElement.start_entity)
                    viewer.entities.remove(graphElement.end_entity)
                }
                break
            // 综合测量
            case 'high':
                viewer.entities.remove(graph.line_entity)
                viewer.entities.remove(graph.height_start)
                viewer.entities.remove(graph.height_end)
                viewer.entities.remove(graph.angle_star_entity)
                viewer.entities.remove(graph.angle_end_entity)
                viewer.entities.remove(graph.length_entity)
                viewer.entities.remove(graph.height_start_label)
                viewer.entities.remove(graph.height_end_label)
                viewer.entities.remove(graph.pedal_entity)
                viewer.entities.remove(graph.start_entity)
                viewer.entities.remove(graph.end_entity)
                break
            // 高度测量
            case 'perpendicularity':
                viewer.entities.remove(graph.height_start)
                viewer.entities.remove(graph.pedal_entity)
                viewer.entities.remove(graph.height_end_label)
                viewer.entities.remove(graph.end_entity)
                viewer.entities.remove(graph.start_entity)
                viewer.entities.remove(graph.height_end)
                break
            // 面积测量
            case 'area':
                viewer.entities.remove(graph.area_label)
                viewer.entities.remove(graph.area_entity)
                for (let lengthElement of graph.Length) {
                    viewer.entities.remove(lengthElement)
                }
                for (let areaPointElement of graph.area_point) {
                    viewer.entities.remove(areaPointElement)
                }
                break
        }
    }
    entity_arry.clear()
}

/**
 * 退出测量操作
 */
const exit_Measurement = () => {
    line_click.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_CLICK)
    line_moce.removeInputAction(Cesium.ScreenSpaceEventType.MOUSE_MOVE)
    line_reight.removeInputAction(Cesium.ScreenSpaceEventType.RIGHT_DOWN)
    EDITOR_click.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_CLICK)
    EDITOR_moce.removeInputAction(Cesium.ScreenSpaceEventType.MOUSE_MOVE)
    EDITOR_reight.removeInputAction(Cesium.ScreenSpaceEventType.RIGHT_DOWN)
}


/**
 * 获取坐标
 * @param Position
 * @returns {*}
 */
const getLocation = (Position) => {
    // 获取模型的坐标
    let pick = viewer.scene.pickPosition(Position)
    let pickModel = viewer.scene.pick(Position)
    if (pickModel && pick && !pickModel.id) {
        let height = Cesium.Cartographic.fromCartesian(pick).height;
        let lat = Cesium.Math.toDegrees(Cesium.Cartographic.fromCartesian(pick).latitude);
        let lng = Cesium.Math.toDegrees(Cesium.Cartographic.fromCartesian(pick).longitude);
        let cartesian = Cesium.Cartesian3.fromDegrees(lng, lat, height);
        return cartesian
    } else {
        // 如果是选中的点 线 标签等 返回false
        if(pickModel && pickModel.id){
            return false
        }
        // 获取椭圆或地图的坐标
        let cartesian = viewer.camera.pickEllipsoid(Position,viewer.scene.globe.ellipsoid)
        if(cartesian){
            return cartesian
        }else{
            // 获取地形坐标
            let ray1 = viewer.camera.getPickRay(Position)
            let cartesian = viewer.scene.globe.pick(ray1, viewer.scene)
            return cartesian
        }
    }

}
